package com.xiefengf.rabbitmq.consumer.service;

import com.rabbitmq.client.Channel;
import com.xiefengf.rabbitmq.consumer.utils.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

/**
 * @author xiefengf
 * @version 1.0
 * @date 2024/1/10 23:58
 *
 * 死信队列消费类
 */

@Service
public class DlxConsumerService {

    /**
     * @RabbitHandler 代表此方法是一个消息接收的方法。该不要有返回值
     * @RabbitListener 可作用于类上，即是整个类被监听，作用于方法上，此方法监听
     * @param msg
     */
    @RabbitListener(queues = {StringUtils.DIRECT_DLX_QUEUE_NAME_1})
    @RabbitHandler
    public void dlxService(@Payload String msg, @Headers Map<String , Object> arguments, Message message, Channel channel) throws IOException {
        // 此处省略发邮件的逻辑
        try {
            System.out.println("dlx-----msg--------->" + msg);
            System.out.println("dlx-----arguments--------->" + arguments);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
        }
    }

}
